from PyQt4 import QtCore, QtGui;
import pickle;

class DataSettings(QtCore.QObject):
    def __init__(self, dataCtrl):
        super(DataSettings, self).__init__();
        self.dataView = dataCtrl.dataView;
        self.mainWin = dataCtrl.parent.mainWin;
        self.parent = dataCtrl.parent;
        self.sFilePath = "";
        self.sDir = "C:/Users/Owner/workspace/Visual_B3/src/StoredData/";
        self.sExt = "Data Files (*.opc)";
        
    def saveSettings(self):
        sFilePath = "";
        sTitle = "Save Settings...";
        if self.restoreDocks():
            sFilePath = QtGui.QFileDialog.getSaveFileName(self.mainWin, sTitle, self.sDir, self.sExt);
        if sFilePath == "": return;
        aDataSettings = [];
        aDataSettings.append(self.parent.layoutSettings.getDataSettings());
        aDataSettings.append(self.parent.graphSettings.getDataSettings());
        aDataSettings.append(self.parent.stTraceEvents.getDataSettings()); 
#         aDataSettings.append(self.dataView.gridStInfo.getDataSettings()); 
#         aDataSettings.append(self.dataView.gridMdpRiskInfo.getDataSettings());
        aDataSettings.append(self.parent.actionCmds.getDataSettings());
        aDataSettings.append(self.parent.graphicsView.getZoom());        
        aDataSettings.append(self.getDockSettings());
        pickle.dump(aDataSettings, open(sFilePath, "w"));
        
    def loadSettings(self, sFilePath=""):
        sTitle = "Load Settings...";
        if self.restoreDocks() and sFilePath=="":
            sFilePath = QtGui.QFileDialog.getOpenFileName(self.mainWin, sTitle, self.sDir, self.sExt);
        if sFilePath == "": return;
        aDataSettings = pickle.load(open(sFilePath));
        self.parent.layoutSettings.setDataSettings(aDataSettings[0]);
        self.parent.graphSettings.setDataSettings(aDataSettings[1]);
        self.parent.stTraceEvents.setDataSettings(aDataSettings[2]);
#         self.dataView.gridStInfo.setDataSettings(aDataSettings[3]);
#         self.dataView.gridMdpRiskInfo.setDataSettings(aDataSettings[4]); 
        self.parent.actionCmds.setDataSettings(aDataSettings[5]);
        self.parent.graphicsView.setZoom(aDataSettings[6]);
        self.setDockSettings(aDataSettings[7]);
    
    def getDockSettings(self):
        aDockSettings = [];
        for dockWidget in self.parent.all_DockWidgets:
            aDockRow = [];
            aDockRow.append(dockWidget.isVisible());
            aDockRow.append(dockWidget.getDockLocation());
            aDockSettings.append(aDockRow);
        return aDockSettings;
    
    def setDockSettings(self, aDockSettings):
        for i in range(len(aDockSettings)):
            self.parent.all_DockWidgets[i].setVisible(aDockSettings[i][0]);
            self.parent.all_DockWidgets[i].setDockLocation(aDockSettings[i][1]);
            
    def restoreDocks(self):
        bDocksReady = True
        for i in range(len(self.parent.all_DockWidgets)):
            if self.parent.all_DockWidgets[i].getUndocked():
                bDocksReady = False;
            if self.parent.all_DockWidgets[i].getDockMaximized():
                bDocksReady = False;
        if not bDocksReady:
            sMsg = "All docks must be restored before saving or loading settings.";
            sTitle = "Restore Docks"
            msgBtns = (QtGui.QMessageBox.Ok | QtGui.QMessageBox.Cancel);
            bAns = QtGui.QMessageBox.question(self.mainWin, sTitle, sMsg, msgBtns);
            if bAns == QtGui.QMessageBox.Ok:
                for i in range(len(self.parent.all_DockWidgets)):
                    if self.parent.all_DockWidgets[i].getDockMaximized():
                        self.parent.all_DockWidgets[i].minimizeDock();
                for i in range(len(self.parent.all_DockWidgets)):
                    if self.parent.all_DockWidgets[i].getUndocked():
                        self.parent.all_DockWidgets[i].restoreDocked();
                return True;
            else:
                return False;
        return True;